package com.marktotop.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.marktotop.entity.business.ObserveRecord;
import com.marktotop.entity.business.VaccinationRecord;
import com.marktotop.error.SystemCodeEnum;
import com.marktotop.error.SystemException;
import com.marktotop.mapper.ObserveRecordMapper;
import com.marktotop.response.ResultBean;
import com.marktotop.response.ResultCode;
import com.marktotop.service.IObserveRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.marktotop.service.IReservationRecordService;
import com.marktotop.service.IVaccinationRecordService;
import com.marktotop.vo.business.InoculateVO;
import com.marktotop.vo.business.ObserveVO;
import com.marktotop.vo.business.ReservationRecordVO;
import com.marktotop.vo.business.VaccineVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author MarkToTop
 * @since 2021-07-12
 */
@Service
public class ObserveRecordServiceImpl extends ServiceImpl<ObserveRecordMapper, ObserveRecord> implements IObserveRecordService {

    @Autowired
    private ObserveRecordMapper observeRecordMapper;

    @Autowired
    private IReservationRecordService reservationRecordService;

    @Autowired
    private IVaccinationRecordService vaccinationRecordService;

    /**
     * 所有接种后留观列表
     *
     * @param pageNum
     * @param pageSize
     * @param observeVO
     * @return
     */
    @Override
    public ResultBean findObserveList(Integer pageNum, Integer pageSize, ObserveVO observeVO){
        // 开启分页
        Page<ObserveVO> page = new Page<>(pageNum,pageSize);
        IPage<ObserveVO> observesByPage = observeRecordMapper.getObservesByPage(page,observeVO);
        if (null == observesByPage){
            return ResultBean.result(ResultCode.NON_RESULT);
        }
        return ResultBean.ok().message("获取所有接种后留观信息成功！")
                .data("total",observesByPage.getTotal())
                .data("observesList",observesByPage.getRecords());
    }


    /**
     * 根据接种id查询某一留观列表
     *
     * @param id
     * @return
     * @throws SystemException
     */
    @Override
    public ObserveVO findOneObserve(Long id) throws SystemException {
        ObserveVO observeInfo = observeRecordMapper.selectOneObserveInfo(id);
        if (null == observeInfo){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"未找到相关的接种信息...");
        }
        return observeInfo;
    }

    /**
     * 提交留观信息，进行留观记录
     *
     * @param observeRecord
     * @return
     * @throws SystemException
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean postObserveRecord(ObserveRecord observeRecord) throws SystemException, ParseException {
        // 接种id
        Long injectionId = observeRecord.getInjectionId();
        // 预约id
        Long reservationId = observeRecord.getReservationId();
        //查询接种信息
        InoculateVO oneInoculateRecord = vaccinationRecordService.getOneInoculateRecord(reservationId);
        // 接种后预约状态不对
        if (oneInoculateRecord.getStates() < 2){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"该接种人的预约信息状态有误,不能继续进行留观提交！");
        }
        //判断留观时间够不够30分钟
        VaccinationRecord vaccinationRecord = vaccinationRecordService.getById(injectionId);
        if (null == vaccinationRecord){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"未找到相关的接种记录！");
        }
        // 后端接种时间- 增加30分钟
        Calendar c = Calendar.getInstance();
        c.setTime(vaccinationRecord.getAccountTime());
        c.add(Calendar.MINUTE, 30);
        // 判断前端记录时间是否 >  后端接种时间 + 30分钟
        if (observeRecord.getAccountTime().before(c.getTime())){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"留观时间未满30分钟，请稍后再进行记录！");
        }
        long time1 = System.currentTimeMillis();
        long time2 = vaccinationRecord.getAccountTime().getTime();
        // 添加留观记录
        observeRecord.setObservationTime((int) ((time1 - time2)/60000));
        if ( 0 == observeRecordMapper.insert(observeRecord)){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"添加留观记录失败，请稍后再试！");
        }
        // 修改预约记录的状态 --  预约号 - 状态值
        if (!reservationRecordService.updateRecordStates(reservationId, oneInoculateRecord.getStates() + 1)){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"更新接种状态失败，请稍后再试！");
        }
        return true;
    }

}
